Skip to main content

@palco-2d/core / BaseEntity

Class: BaseEntity

Defined in: src/BaseEntity.ts:31

BaseEntity serves as the foundational class for all entities managed by the rendering and mouse handling systems. It encapsulates the essential properties and methods shared across all entities, ensuring seamless integration with rendering and interactive functionalities. Any custom entity should extend this class to be properly managed by the engine, maintaining consistency in interactivity and rendering behavior

Extended by

Constructors

new BaseEntity()

new BaseEntity(props): BaseEntity

Defined in: src/BaseEntity.ts:252

Parameters

props

BaseEntityProps

Returns

BaseEntity

Properties

children

children: Map<number, Map<string, BaseEntity>>

Defined in: src/BaseEntity.ts:162

A map representing the children organized by layers.

[
[layer]: [
[id]: BaseEntity
]
]

coords

coords: Coords

Defined in: src/BaseEntity.ts:103

The coordinates of the entity in the canvas. represents the for corners of the entity in the canvas, and the bounding box of that these corners describes.


globalCompositeOperation

globalCompositeOperation: GlobalCompositeOperation = "source-over"

Defined in: src/BaseEntity.ts:222

The globalCompositeOperation property sets the type of compositing operation to apply when drawing the render method. The default value is source-over.


id

id: string

Defined in: src/BaseEntity.ts:35

The unique id of the entity.


isMouseHover

isMouseHover: boolean

Defined in: src/BaseEntity.ts:141

If the mouse is hovering the entity it returns true, otherwise false.


isUI

isUI: boolean = false

Defined in: src/BaseEntity.ts:216

When isUI is set to true the entity will be render in the upper canvas, when the mouse detection will preoritize the UI entities over others.


layer

layer: number

Defined in: src/BaseEntity.ts:150

The layer of the entity represents the order the render handler will render the entity. 0 is the first layer, and the entities with higher layers will be rendered on top of the entities with lower layers. The layer is relative to the parent, so if the entity is a child, the layer is relative to the parent. If the entity has a layer equal to 0, and the parent has a layer equal to 10, any entity with layer less than 10, that's not a child of a parent, will be rendered before.


mask

mask: Mask

Defined in: src/BaseEntity.ts:174

Mask Handler to handle the masking of the entity.


matrix

matrix: number[][] = []

Defined in: src/BaseEntity.ts:116

The matrix of the entity, used to calculate the transformation of the entity.


onEntityEvent

onEntityEvent: EntityEvents

Defined in: src/BaseEntity.ts:169

A record of events that the entity has subscribed to. Mouse handler will trigger these events depending on the interaction with the entity. if the entity is static, the mouse handler will not trigger any event.


quadrant

quadrant: EntityQuadrant

Defined in: src/BaseEntity.ts:233

The canvas is divided into quadrants, and each entity is assigned to a quadrant. these quadrants are used to optimize the mouse detection over the entities, preventing the mouse checking for all entities in the canvas, it will only check for the ones within the same quadrant as the mouse. quadrant represents all the quadrants the entity is assigned, depending on the transformation of the entity, it can have assigned multiple quadrants. The quadrant updated when the entity matrix changes.

Accessors

parent

Get Signature

get parent(): BaseEntity

Defined in: src/BaseEntity.ts:197

Returns

BaseEntity

Set Signature

set parent(parent): void

Defined in: src/BaseEntity.ts:201

Parameters
parent

BaseEntity

Returns

void


position

Get Signature

get position(): Vec2

Defined in: src/BaseEntity.ts:45

The position of the entity in the canvas, relative to the top-left corner. if the entity is a child, the position is relative to the parent.

Returns

Vec2

Set Signature

set position(position): void

Defined in: src/BaseEntity.ts:49

Parameters
position

Vec2

Returns

void


realSize

Get Signature

get realSize(): Vec2

Defined in: src/BaseEntity.ts:94

The size of the element property (not considering canvas transformation)

Returns

Vec2

Vec2


rotation

Get Signature

get rotation(): number

Defined in: src/BaseEntity.ts:124

Returns

number

Set Signature

set rotation(rotation): void

Defined in: src/BaseEntity.ts:128

Parameters
rotation

number

Returns

void


size

Get Signature

get size(): Vec2

Defined in: src/BaseEntity.ts:67

The size of the entity in the canvas. if the entity is a child, the size is relative to the parent.

Returns

Vec2

Set Signature

set size(size): void

Defined in: src/BaseEntity.ts:75

Parameters
size

Vec2

Returns

void


useAsMask

Get Signature

get useAsMask(): boolean

Defined in: src/BaseEntity.ts:180

When set to true children elements will be masked Note: When using and element as a mask globalCompositeOperation does not take effect

Returns

boolean

Set Signature

set useAsMask(enable): void

Defined in: src/BaseEntity.ts:184

Parameters
enable

boolean

Returns

void

Methods

addChild()

addChild(child): void

Defined in: src/BaseEntity.ts:475

Parameters

child

BaseEntity

Returns

void


destroy()

destroy(): void

Defined in: src/BaseEntity.ts:504

Returns

void


getCoords()

protected getCoords(): Coords

Defined in: src/BaseEntity.ts:354

Gets the coords of the entity in the canvas. the coords are the corners of the entity in the canvas. eg: top-left, top-right, bottom-right, bottom-left

Returns

Coords


getIdAddress()

getIdAddress(): string

Defined in: src/BaseEntity.ts:604

Get the string address of the entity. With the address id you can get any entity in the scene, it represents the path to the entity.

Returns

string

Example

{layer}:{id}/{layer}:{id}/{layer}:{id}

the first layer and id is the entity itself, the next layers and ids are the parents of the entity.

getMatrix()

protected getMatrix(): number[][]

Defined in: src/BaseEntity.ts:337

Gets the transformation matrix of the entity,

Returns

number[][]


getOwnAddress()

getOwnAddress(): string

Defined in: src/BaseEntity.ts:614

Returns

string


getPivotPosition()

getPivotPosition(): object

Defined in: src/BaseEntity.ts:293

Returns

object

x

x: number

y

y: number


getRelativePosition()

getRelativePosition(mousePosition, relativeToParent?): object

Defined in: src/BaseEntity.ts:419

Parameters

mousePosition

Vec2

relativeToParent?

boolean

Returns

object

x

x: number

y

y: number


getScale()

getScale(): object

Defined in: src/BaseEntity.ts:279

Returns

object

x

x: number = 1

y

y: number = 1


getStatic()

getStatic(): boolean

Defined in: src/BaseEntity.ts:623

Returns

boolean


getWorldMatrix()

getWorldMatrix(): number[][]

Defined in: src/BaseEntity.ts:317

Returns

number[][]


isObjectInViewport()

isObjectInViewport(viewport): boolean

Defined in: src/BaseEntity.ts:550

Parameters

viewport
position

Vec2

size

Vec2

Returns

boolean


isPointOverEntity()

isPointOverEntity(point): boolean

Defined in: src/BaseEntity.ts:527

Check if the point is over the entity.

Parameters

point

Vec2

Returns

boolean


on()

on(event, callback): void

Defined in: src/BaseEntity.ts:274

Parameters

event

EventsType

callback

() => void

Returns

void


removeChild()

removeChild(address): void

Defined in: src/BaseEntity.ts:492

Parameters

address

string

Returns

void


render()

render(ctx): void

Defined in: src/BaseEntity.ts:300

Parameters

ctx

CanvasRenderingContext2D

Returns

void


serialize()

serialize(): SerializedBaseEntityProps

Defined in: src/BaseEntity.ts:573

Returns

SerializedBaseEntityProps


setStatic()

setStatic(staticValue): void

Defined in: src/BaseEntity.ts:618

Parameters

staticValue

boolean

Returns

void